package com.webank.wedatasphere.qualitis.handler;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.webank.wedatasphere.qualitis.project.constant.ExcelSheetName;
import com.webank.wedatasphere.qualitis.project.dao.repository.ProjectMuLuRepository;
import com.webank.wedatasphere.qualitis.project.dao.repository.ProjectRepository;
import com.webank.wedatasphere.qualitis.project.entity.ProjectMuLu;
import com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject;
import com.webank.wedatasphere.qualitis.response.BiaoZunExcelDTO;
import com.webank.wedatasphere.qualitis.response.Resp;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class CommonExcelService {

    private Logger logger = LoggerFactory.getLogger(CommonExcelService.class);
    @Autowired
    private ProjectRepository repository;

    @Autowired
    private ProjectMuLuRepository projectMuLuRepository;

    /**
     * 标准的导入Excel
     *
     * @param fileSavePath
     * @return
     */
    public Resp<?> handlerBiaoZunUpload(String fileSavePath) {
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        try {
            logger.info("附件上传excel===》" + fileSavePath);
            FileInputStream fileInputStream = new FileInputStream(fileSavePath);
            ExcelReader excelReader = new ExcelReader(fileInputStream, null, new ExcelBiaoZunListener(repository, projectMuLuRepository));
            List<Sheet> sheets = excelReader.getSheets();
            Sheet sheet = sheets.get(0);
            sheet.setClazz(BiaoZunExcelDTO.class);
            sheet.setHeadLineMun(1);
            excelReader.read(sheet);
            logger.info("读取完成excel===》" + fileSavePath);
        } catch (Exception e) {
            e.printStackTrace();
            return Resp.error(e.getMessage());
        }
        return Resp.Ok();
    }

    /**
     * 标准的导出Excel
     *
     * @param outputStream
     * @return
     */
    public void handlerBiaoZunDownload(OutputStream outputStream, List<BiaoZunExcelDTO> list) throws IOException {
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        try {
            List<ProjectMuLu> muLuRepositoryAll = projectMuLuRepository.findAll();
            for (BiaoZunExcelDTO biaoZunExcelDTO : list) {
                for (ProjectMuLu muLu : muLuRepositoryAll) {
                    if (StringUtils.isNotBlank(biaoZunExcelDTO.getSjzlbzml()) && biaoZunExcelDTO.getSjzlbzml().equals(muLu.getId().toString())) {
                        biaoZunExcelDTO.setSjzlbzml(muLu.getTitle());
                    }
                }
            }
            ExcelWriter writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX, true);
            Sheet templateRuleSheet = new Sheet(1, 0, BiaoZunExcelDTO.class);
            templateRuleSheet.setSheetName("导出标准");
            List<List<String>> heads = new ArrayList<>();
            List<String> head1 = new ArrayList<>();
            List<String> head2 = new ArrayList<>();
            List<String> head3 = new ArrayList<>();
            List<String> head4 = new ArrayList<>();
            List<String> head5 = new ArrayList<>();
            List<String> head6 = new ArrayList<>();
            head1.add("数据质量名称");
            head2.add("适用范围");
            head3.add("数据质量类型");
            head4.add("参考文献");
            head5.add("所属质量标准目录");
            head6.add("发布状态");
            heads.add(head1);
            heads.add(head2);
            heads.add(head3);
            heads.add(head4);
            heads.add(head5);
            heads.add(head6);
            templateRuleSheet.setHead(heads);
            writer.write(list, templateRuleSheet);
            writer.finish();
            logger.info("写入完成excel===》");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            outputStream.close();
        }
    }

}
